package com.controller.front_controller;


import com.mypg.UserHistory;
import com.service.UserHistoryService;
import org.apache.commons.fileupload.util.Streams;
import org.apache.ibatis.javassist.Loader;
import org.bytedeco.javacv.FFmpegFrameGrabber;
import org.bytedeco.javacv.Frame;
import org.bytedeco.javacv.FrameGrabber;
import org.bytedeco.javacv.Java2DFrameConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.SimpleDriverDataSource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Base64;


@RequestMapping("/front_test")
@Controller
public class FrontTestController {

    @Autowired
    UserHistoryService userHistoryService;

    @RequestMapping("/test_index.do")
    @ResponseBody
    public int doTest_index(int user_id,String history_class){
        int n=0;
        try{
            n=userHistoryService.selectIndexByUser_idAndHistory_class(user_id,history_class);
        }catch (Exception e){
            System.out.println("do something");
        }
        return n;
    }

    @RequestMapping("/test12_test_video_time.do")
    @ResponseBody
    public String doTest12_test_video_time(){
        File video=new File("E:\\毕设有关\\上传的文件\\自己上传的视频\\科二学习视频\\ke2_cefantingche.mp4");
        long duration = 0L;
        FFmpegFrameGrabber ff = new FFmpegFrameGrabber(video);
        try {
            ff.start();
            duration = ff.getLengthInTime() / (1000 * 1000);
            ff.stop();
        } catch (FrameGrabber.Exception e) {
            e.printStackTrace();
        }
        String res="";
        int mm= (int) Math.floor(duration/60);
        int ss= (int) (duration-(mm*60));
        res=mm+":"+ss;


        return res;
    }

    @RequestMapping("/test12_test_video_img.do")
    @ResponseBody
    public String doTest12_test_video_img(@RequestParam(value = "file_video",required = false)MultipartFile file_video) throws IOException {
        String picPath="E:\\毕设有关\\上传的文件\\图片\\video_img\\";
        picPath+=file_video.getOriginalFilename().replace(".","_")+".jpg";
        File video=new File("E:\\毕设有关\\上传的文件\\图片\\video_img\\test.mp4");
        Streams.copy(file_video.getInputStream(),new FileOutputStream(video.getAbsolutePath()),true);
        FFmpegFrameGrabber ff = new FFmpegFrameGrabber(video);
        try {
            ff.start();
            // 截取中间帧图片(具体依实际情况而定)
            int i = 0;
            int length = ff.getLengthInFrames();
            int middleFrame = length / 2;
            Frame frame = null;
            while (i < length) {
                frame = ff.grabFrame();
                if ((i > middleFrame) && (frame.image != null)) {
                    break;
                }
                i++;
            }
            // 截取的帧图片
            Java2DFrameConverter converter = new Java2DFrameConverter();
            BufferedImage srcImage = converter.getBufferedImage(frame);
            int srcImageWidth = srcImage.getWidth();
            int srcImageHeight = srcImage.getHeight();
            // 对截图进行等比例缩放(缩略图)
            int width = 480;
            int height = (int) (((double) width / srcImageWidth) * srcImageHeight);
            BufferedImage thumbnailImage = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
            thumbnailImage.getGraphics().drawImage(srcImage.getScaledInstance(width, height, Image.SCALE_SMOOTH), 0, 0, null);
            File picFile = new File(picPath);
            ImageIO.write(thumbnailImage, "jpg", picFile);
            ff.stop();
        } catch (IOException e) {
            e.printStackTrace();
        }
//        video.delete();
        return "";
    }

}
